package grpcontroller

import (
	"context"
	"encoding/json"
	"fmt"
	"golang_start/app/api/service"
	"golang_start/common"
	"golang_start/grpc/gogrpc"
)

type CodeActivityTestaGrpc struct {
	gogrpc.UnimplementedCodeActivityTestaGrpcServer
}

/*增删改查*/
func (model *CodeActivityTestaGrpc) Add_(ctx context.Context, data *gogrpc.ParamRequest) (*gogrpc.DataResponse, error) {
	resp := new(gogrpc.DataResponse)
	var param = &service.CodeActivityTesta{}
	err := json.Unmarshal([]byte(data.Request), param)
	if err != nil {
		fmt.Println("json解析错误", err.Error())
	}
	result, err := (&service.CodeActivityTesta{}).Add_(ctx, param)
	json_result := make(map[string]interface{})
	if err != nil {
		json_result["code"] = common.FAIL_CODE
		json_result["msg"] = "Fail" + err.Error()
	} else {
		json_result["code"] = common.SUCCESS_CODE
		json_result["msg"] = "操作成功"
	}

	json_result["data"] = result

	respResponse, _ := json.Marshal(json_result)
	resp.Response = string(respResponse)
	fmt.Println("返回Grpc接口结果")
	return resp, nil

}
func (model *CodeActivityTestaGrpc) Delete_(ctx context.Context, data *gogrpc.ParamRequest) (*gogrpc.DataResponse, error) {
	resp := new(gogrpc.DataResponse)
	var delete_ids = &service.CodeActivityTestaDeleteids{}
	err := json.Unmarshal([]byte(data.Request), delete_ids)
	if err != nil {
		fmt.Println("json解析错误", err.Error())
	}
	result, err := (&service.CodeActivityTesta{}).Delete_(ctx, delete_ids)
	json_result := make(map[string]interface{})
	if err != nil {
		json_result["code"] = common.FAIL_CODE
		json_result["msg"] = "Fail" + err.Error()
	} else {
		json_result["code"] = common.SUCCESS_CODE
		json_result["msg"] = "操作成功"
	}

	json_result["data"] = result

	respResponse, _ := json.Marshal(json_result)
	resp.Response = string(respResponse)
	return resp, nil
}
func (model *CodeActivityTestaGrpc) Update_(ctx context.Context, data *gogrpc.ParamRequest) (*gogrpc.DataResponse, error) {
	resp := new(gogrpc.DataResponse)
	var param = &service.CodeActivityTesta{}
	err := json.Unmarshal([]byte(data.Request), param)
	if err != nil {
		fmt.Println("json解析错误", err.Error())
	}
	fmt.Println(param)
	result, err := (&service.CodeActivityTesta{}).Update_(ctx, param)
	json_result := make(map[string]interface{})
	if err != nil {
		json_result["code"] = common.FAIL_CODE
		json_result["msg"] = "Fail" + err.Error()
	} else {
		json_result["code"] = common.SUCCESS_CODE
		json_result["msg"] = "成功"
	}
	json_result["data"] = result

	respResponse, _ := json.Marshal(json_result)
	resp.Response = string(respResponse)
	fmt.Println("返回Grpc接口结果")
	return resp, nil
}
func (model *CodeActivityTestaGrpc) List_(ctx context.Context, data *gogrpc.ParamRequest) (*gogrpc.DataResponse, error) {
	resp := new(gogrpc.DataResponse)
	json_param := make(map[string]string)
	err := json.Unmarshal([]byte(data.Request), &json_param)
	if err != nil {
		fmt.Println("json解析错误", err.Error())
	}
	_, exists_pindex := json_param["pindex"]
	if !exists_pindex {
		json_param["pindex"] = "1"
	}
	_, exists_psize := json_param["psize"]
	if !exists_psize {
		json_param["psize"] = "10"
	}
	_, exists_order := json_param["order"]
	if !exists_order {
		json_param["order"] = "id"
	}
	_, exists_order_sort := json_param["order_sort"]
	if !exists_order_sort {
		json_param["order_sort"] = "desc"
	}

	result, count, err := (&service.CodeActivityTesta{}).List_(ctx, json_param)
	json_result := make(map[string]interface{})
	if err != nil {
		json_result["code"] = common.FAIL_CODE
		json_result["msg"] = "Fail" + err.Error()
	} else {
		json_result["code"] = common.SUCCESS_CODE
		json_result["msg"] = "成功"
	}

	json_result["data"] = result
	json_result["count"] = count
	respResponse, _ := json.Marshal(json_result)
	resp.Response = string(respResponse)
	fmt.Println("返回Grpc接口结果")

	return resp, nil
}
func (model *CodeActivityTestaGrpc) Detail_(ctx context.Context, data *gogrpc.ParamRequest) (*gogrpc.DataResponse, error) {
	resp := new(gogrpc.DataResponse)
	var param = &service.CodeActivityTesta{}
	err := json.Unmarshal([]byte(data.Request), param)
	if err != nil {
		fmt.Println("json解析错误", err.Error())
	}
	result, err := (&service.CodeActivityTesta{}).Detail_(ctx, param)
	if err != nil {
		fmt.Println("查询错误", err.Error())
	}

	json_result := make(map[string]interface{})
	json_result["code"] = common.SUCCESS_CODE
	json_result["msg"] = "成功"
	json_result["data"] = result

	respResponse, _ := json.Marshal(json_result)
	resp.Response = string(respResponse)
	fmt.Println("返回Grpc接口结果")
	return resp, nil
}

/*增加对应的json格式解析方法*/
